/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui.view;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.model.elements.Tone;
import percussiongenerator.model.Track;
import percussiongenerator.sound.IMidiPlayer;

/**
 *
 * @author jannes
 */
public class MetronomeView extends TimeLineView{
    private IMidiPlayer midiPlayer;
    private ArrayList<Long> delays;
    public MetronomeView(Track track,MutableValueNode<View> selectedView,IMidiPlayer midiPlayer){
        super(track,selectedView,new BasicValueNode<ArrayList<Integer>>(null));
        viewOptions = new MetronomeViewOptions(track);
        this.midiPlayer = midiPlayer;
        //heightNode.setValue(50);
        //widthNode.setValue(400);
    }

    @Override
    public void mousePressedExtra(MouseEvent e){
        delays = new ArrayList<Long>();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(!e.isActionKey() && e.getKeyCode() != KeyEvent.VK_ENTER){
            long timeMidiPlayer = midiPlayer.getTime();
            long intervalLength = track.getStaff().getToneSequence().getPerformanceSequence().getIntervalFromIndex(0).getDifference();
            long delay = timeMidiPlayer%intervalLength;
            delays.add(delay);
        }else if(e.getKeyCode() == KeyEvent.VK_ENTER && e.getKeyCode() != KeyEvent.VK_M){
            if(!delays.isEmpty()){
                //calculate median;
                long sum = 0;
                for(int i = 0; i < delays.size(); i++){
                    sum += delays.get(i);
                }
                long median = sum/delays.size();
                //remove spikes
                sum = 0;
                int numberOfElements = 0;
                long allowedError = (long)(0.25 * median);
                for(int i = 0; i < delays.size(); i++){
                    if(Math.abs(delays.get(i)-median) < allowedError){
                        sum += delays.get(i);
                        numberOfElements++;
                    }
                }
                median = sum/numberOfElements;
                midiPlayer.setSoundDelay(median);
            }
            delays = new ArrayList<Long>();
        }
        if(e.getKeyCode() == KeyEvent.VK_M){
            if(track.isMuted()){
                track.setMute(false);
            }else{
                track.setMute(true);
            }
        }        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_LEFT){
            midiPlayer.setSoundDelay(midiPlayer.getSoundDelay() + track.getStaff().getToneSequence().getPerformanceSequence().getIntervalFromIndex(0).getDifference());
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            midiPlayer.setSoundDelay(midiPlayer.getSoundDelay() + track.getStaff().getToneSequence().getPerformanceSequence().getIntervalFromIndex(0).getDifference());
        }
    }

    @Override
    public void mouseReleasedExtra(MouseEvent e){}
    @Override
    public void mouseDragged(MouseEvent e) {}
    @Override
    public void mouseMoved(MouseEvent e) {}

    /*
    @Override
    public void paintView(Graphics g) {        
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, getWidth()-1, getHeight()-1);
        String text = "BPM: " + 120000000000l/track.getPerformanceLength();
        int stringWidth = g.getFontMetrics().stringWidth(text);
        g.drawString(text, (getWidth()-stringWidth)/2, getHeight()/2);
        long time = track.getTime();
        int offset;
        if(time < track.getPerformanceLength()/2){
            offset = (int)(time*(getWidth()-getHeight())/(track.getPerformanceLength()/2));
            g.fillOval(offset, 0, getHeight(), getHeight());
        }else{
            offset = (int)((time-track.getPerformanceLength()/2)*(getWidth()-getHeight())/(track.getPerformanceLength()/2));
            g.fillOval(getWidth()-getHeight()-offset, 0, getHeight(), getHeight());
        }                
    }

    public void updateLanguage(ResourceBundle labels) {        
    }

    public void addElements(JPopupMenu popup, Object caller) {
        
    }
    */    
    
}
